package com.lzs.adx.web.modules.adx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzs.adx.commmon.utils.DateUtils;
import com.lzs.adx.web.modules.adx.dao.TotalStatisticsDao;
import com.lzs.adx.web.modules.adx.dto.TotalStatisticsDTO;
import com.lzs.adx.web.modules.adx.entity.LoanReportDayEntity;
import com.lzs.adx.web.modules.adx.entity.TotalStatisticsEntity;
import com.lzs.adx.web.modules.adx.service.TotalStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 汇总统计表
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2024-09-10
 */
@Service
public class TotalStatisticsServiceImpl extends CrudServiceImpl<TotalStatisticsDao, TotalStatisticsEntity, TotalStatisticsDTO> implements TotalStatisticsService {

    @Override
    public QueryWrapper<TotalStatisticsEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String product = (String)params.get("product");
        String partyChannel= (String)params.get("partyChannel");
        String secondChannel= (String)params.get("secondChannel");
        String directBusiness= (String)params.get("directBusiness");
        String operate= (String)params.get("operate");
        String type= (String)params.get("type");
        String stime= (String)params.get("stime");
        String etime= (String)params.get("etime");

        QueryWrapper<TotalStatisticsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(product), "product", product);
        wrapper.like(StringUtils.isNotBlank(partyChannel), "party_channel", partyChannel);
        wrapper.like(StringUtils.isNotBlank(secondChannel), "second_channel", secondChannel);
        wrapper.eq(StringUtils.isNotBlank(directBusiness), "direct_business", directBusiness);
        wrapper.eq(StringUtils.isNotBlank(operate), "operate", operate);
        wrapper.eq(StringUtils.isNotBlank(type), "type", type);
        wrapper.ge(StringUtils.isNotBlank(stime), "day", stime);
        wrapper.le(StringUtils.isNotBlank(etime), "day", etime);

        wrapper.orderByDesc("update_time").orderByDesc("day");

        return wrapper;
    }

    @Override
    public void save(TotalStatisticsDTO totalStatisticsDTO) {
        TotalStatisticsEntity newValue = new TotalStatisticsEntity();
        BeanUtils.copyProperties(totalStatisticsDTO,newValue);
        newValue.setMonth(DateUtils.getMonth(totalStatisticsDTO.getDay()));
        newValue.setCreateTime(new Date());
        newValue.setUpdateTime(new Date());
        //分润
        distributeProfits(totalStatisticsDTO,newValue);

        this.insert(newValue);
    }

    @Override
    public void update(TotalStatisticsDTO totalStatisticsDTO) {
        TotalStatisticsEntity newValue = new TotalStatisticsEntity();
        BeanUtils.copyProperties(totalStatisticsDTO,newValue);
        newValue.setMonth(DateUtils.getMonth(totalStatisticsDTO.getDay()));
        newValue.setUpdateTime(new Date());
        newValue.setId(totalStatisticsDTO.getId());
        //分润
        distributeProfits(totalStatisticsDTO,newValue);

        baseDao.updateById(newValue);
    }

    @Override
    public void generateData(TotalStatisticsDTO totalStatisticsDTO) {

        //生成报表
        LambdaQueryWrapper<TotalStatisticsEntity> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(TotalStatisticsEntity::getDay,totalStatisticsDTO.getDay());
        lambdaQueryWrapper.eq(TotalStatisticsEntity::getProduct,totalStatisticsDTO.getProduct());
        lambdaQueryWrapper.eq(TotalStatisticsEntity::getPartyChannel,totalStatisticsDTO.getPartyChannel());
        lambdaQueryWrapper.eq(TotalStatisticsEntity::getSecondChannel,totalStatisticsDTO.getSecondChannel());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(totalStatisticsDTO.getProductCode()), TotalStatisticsEntity::getProductCode,totalStatisticsDTO.getProductCode());

        List<TotalStatisticsEntity> totalStatisticsDTOS = baseDao.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(totalStatisticsDTOS)){
            TotalStatisticsEntity dbTotalStatisticsDTO1 = totalStatisticsDTOS.get(0);
            TotalStatisticsEntity newValue = new TotalStatisticsEntity();
            BeanUtils.copyProperties(totalStatisticsDTO,newValue);
            newValue.setMonth(DateUtils.getMonth(totalStatisticsDTO.getDay()));
            newValue.setUpdateTime(new Date());
            newValue.setId(dbTotalStatisticsDTO1.getId());
            //分润
            distributeProfits(totalStatisticsDTO,newValue);
            baseDao.updateById(newValue);
        }else{
            TotalStatisticsEntity newValue = new TotalStatisticsEntity();
            BeanUtils.copyProperties(totalStatisticsDTO,newValue);
            newValue.setMonth(DateUtils.getMonth(totalStatisticsDTO.getDay()));
            newValue.setCreateTime(new Date());
            newValue.setUpdateTime(new Date());
            //分润
            distributeProfits(totalStatisticsDTO,newValue);

            baseDao.insert(newValue);
        }
    }


    /**
     * 分润逻辑
     * @param totalStatisticsDTO
     * @param newValue
     */
    public static void distributeProfits(TotalStatisticsDTO totalStatisticsDTO,TotalStatisticsEntity newValue) {

        BigDecimal totalProfit=totalStatisticsDTO.getProfit();
        String business=totalStatisticsDTO.getDirectBusiness();
        String secondaryBusiness=totalStatisticsDTO.getJoinBusiness();
        String operations=totalStatisticsDTO.getOperate();
        if (Objects.isNull(totalProfit) && totalProfit.compareTo(BigDecimal.ZERO) <= 0) {
            System.out.println("总利润必须大于0");
            return;
        }

        if (StringUtils.isNotBlank(business) && StringUtils.isNotBlank(operations) && StringUtils.isNotBlank(secondaryBusiness)) {
            // 场景1: 三个字段都有值
            BigDecimal businessProfit = totalProfit.multiply(new BigDecimal("0.40"));
            BigDecimal operationsProfit = totalProfit.multiply(new BigDecimal("0.40"));
            BigDecimal secondaryBusinessProfit = totalProfit.subtract(businessProfit).subtract(operationsProfit);
            newValue.setDirectBusinessProfit(businessProfit);
            newValue.setOperateProfit(operationsProfit);
            newValue.setJoinBusinessProfit(secondaryBusinessProfit);
        } else if (StringUtils.isNotBlank(business) && StringUtils.isNotBlank(operations)) {
            // 场景3: 商务和运营有值
            BigDecimal businessProfit = totalProfit.multiply(new BigDecimal("0.5"));
            BigDecimal operationsProfit = totalProfit.multiply(new BigDecimal("0.5"));
            newValue.setDirectBusinessProfit(businessProfit);
            newValue.setOperateProfit(operationsProfit);

        } else if (StringUtils.isNotBlank(business) && StringUtils.isNotBlank(secondaryBusiness) ) {
          // 场景4：商务和二级商务有值，运营无值
            BigDecimal businessProfit = totalProfit.multiply(new BigDecimal("0.80"));
            BigDecimal secondaryBusinessProfit = totalProfit.subtract(businessProfit);
            newValue.setDirectBusinessProfit(businessProfit);
            newValue.setJoinBusinessProfit(secondaryBusinessProfit);
        } else if (StringUtils.isNotBlank(business) ) {
            newValue.setDirectBusinessProfit(totalProfit);
        }else {
            System.out.println("无法分配利润，请检查输入数据");
        }
    }



}